package com.bw.live_streaming.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.common.utils.PageUtils;
import com.bw.common.utils.Query;

import com.bw.live_streaming.dao.SlideshowDao;
import com.bw.live_streaming.entity.SlideshowEntity;
import com.bw.live_streaming.service.SlideshowService;


@Service("slideshowService")
public class SlideshowServiceImpl extends ServiceImpl<SlideshowDao, SlideshowEntity> implements SlideshowService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SlideshowEntity> page = this.page(
                new Query<SlideshowEntity>().getPage(params),
                new QueryWrapper<SlideshowEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SlideshowEntity> lists() {
        //倒叙查询所有
        List<SlideshowEntity> list=baseMapper.lists();
        //获取redis里面key为show中的值
        Object o = redisTemplate.opsForValue().get("live_streaming-Slideshow");
        //判断redis是否为空
        if (o!=null){
            //不为空就直接查
            System.out.println("去redis里面查了");
            return (List<SlideshowEntity>) o;
        }else{
            //为空就把从数据库倒序查到的数据放入redis中
            System.out.println("去数据库查了");
            //把从数据库查到的值放入redis缓存中
            redisTemplate.opsForValue().set("live_streaming-Slideshow",list,30, TimeUnit.MINUTES);
            //返回
            return list;
        }
    }

}